package learn.advance.objectstream1;


/*
    序列化和反序列化的时候,会抛出NotSerializableException没有序列化异常
    类通过实现 java.io.Serializable 接口以启用其序列化功能。未实现此接口的类将无法使其任何状态序列化或反序列化。
    Serializable接口也叫标记型接口
        要进行序列化和反序列化的类必须实现Serializable接口,就会给类添加一个标记
        当我们进行序列化和反序列化的时候,就会检测类上是否有这个标记
            有:就可以序列化和反序列化
            没有:就会抛出 NotSerializableException异常
    去市场买肉-->肉上有一个蓝色章(检测合格)-->放心购买-->买回来怎么吃随意


    static关键字:静态关键字
        静态优先于非静态加载到内存中(静态优先于对象进入到内存中)
        被static修饰的成员变量不能被序列化的,序列化的都是对象
        private static int age;
        oos.writeObject(new Person("小美女",18));
        Object o = ois.readObject();
        Person{name='小美女', age=0}

    transient关键字:瞬态关键字
        被transient修饰成员变量,不能被序列化
        private transient int age;
        oos.writeObject(new Person("小美女",18));
        Object o = ois.readObject();
        Person{name='小美女', age=0}

 */

import java.io.*;
import java.util.Properties;

class Person implements Serializable {
    private static final long serial_id_ = 1L;
    private String name_;
    public int age1_;
    public transient int age2_;
    public static int age3_;

    public Person(String name, int age1, int age2, int age3) {
        this.name_ = name;
        this.age1_ = age1;
        this.age2_ = age2;
        this.age3_ = age3;
    }

    @Override
    public String toString() {
        return "Person{" + "name_:" + name_ +
                " age1_:" + age1_ +
                " age2_:" + age2_ +
                " age3_:" + age3_ + "}";
    }

    public String GetName() {
        return name_;
    }

    public int GetAge1() {
        return age1_;
    }

    public int GetAge2() {
        return age2_;
    }

    public int GetAge3() {
        return age3_;
    }
}

class Demo2ObjectOutputStream {
    public static void Test() throws IOException {
        ObjectOutputStream oos =
                new ObjectOutputStream(new FileOutputStream("D:\\workplace\\新建文件夹\\test\\a.txt"));

        oos.writeObject(new Person("哈哈哈", 18, 20, 25));
        oos.close();

    }
}

class Demo2ObjectInputStream {
    public static void Test() throws IOException, ClassNotFoundException {
        ObjectInputStream ois =
                new ObjectInputStream(new FileInputStream("D:\\workplace\\新建文件夹\\test\\a.txt"));

        Object o = ois.readObject();
        ois.close();

        System.out.println(o);
        Person p = (Person) o;

        System.out.println("name:" + p.GetName());
        System.out.println("age1:" + p.GetAge1());
        System.out.println("age2:" + p.GetAge2());
        System.out.println("age3:" + p.GetAge3());

    }
}





public class TestObjectStream {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //Demo2ObjectOutputStream.Test();
        Demo2ObjectInputStream.Test();
    }
}
